package com.yunxiao.cloud.job.controller;

import com.yunxiao.cloud.common.base.BaseResultResponse;
import com.yunxiao.cloud.job.entity.RequestScheduleModel;
import com.yunxiao.cloud.job.entity.ScheduleJob;
import com.yunxiao.cloud.job.service.ScheduleJobService;
import io.swagger.annotations.Api;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.Date;

/**
 * @description: ScheduleJobController
 * @author: yunxiao
 * @date: 2020/1/9 16:46
 * @version: V1.0
 **/
@Api(value = "ScheduleJob管理", tags = {"各种封装的任务调度"})
@RestController
@RequestMapping("job")
public class ScheduleJobController {

    @Resource
    private ScheduleJobService scheduleJobService;

    /**
     * 添加定时器
     */
    @PostMapping("/insertJob")
    public BaseResultResponse<ScheduleJob> insertJob(@RequestBody RequestScheduleModel requestScheduleModel) {
        ScheduleJob scheduleJob = new ScheduleJob();
        setScheduleJobEntity(scheduleJob, requestScheduleModel);
        scheduleJob.setCreateTime(new Date());
        scheduleJobService.insert(scheduleJob);
        return BaseResultResponse.ok(scheduleJob);
    }

    /**
     * 执行一次定时器
     */
    @PostMapping("/runJob")
    public BaseResultResponse runJob(@RequestBody RequestScheduleModel scheduleJob) {
        scheduleJobService.run(scheduleJob.getJobId());
        return BaseResultResponse.ok();
    }

    /**
     * 更新定时器
     */
    @PostMapping("/updateJob")
    public BaseResultResponse<Object> updateJob(@RequestBody RequestScheduleModel requestScheduleModel) {
        ScheduleJob scheduleJob = new ScheduleJob();
        setScheduleJobEntity(scheduleJob, requestScheduleModel);
        scheduleJob.setUpdateTime(new Date());
        scheduleJobService.update(scheduleJob);
        return BaseResultResponse.ok();
    }

    private void setScheduleJobEntity(ScheduleJob scheduleJob, RequestScheduleModel requestScheduleModel) {
        scheduleJob.setJobId(requestScheduleModel.getJobId());
        scheduleJob.setStatus(requestScheduleModel.getStatus());
        scheduleJob.setBeanName(requestScheduleModel.getBeanName());
        scheduleJob.setCronExpression(requestScheduleModel.getCronExpression());
        scheduleJob.setParams(requestScheduleModel.getParams());
        scheduleJob.setRemark(requestScheduleModel.getRemark());
    }

    /**
     * 停止定时器
     */
    @PostMapping("/pauseJob")
    public BaseResultResponse<Object> pauseJob(RequestScheduleModel scheduleJob) {
        scheduleJobService.pauseJob(scheduleJob.getJobId());
        return BaseResultResponse.ok();
    }

    /**
     * 恢复定时器
     */
    @PostMapping("/resumeJob")
    public BaseResultResponse<ScheduleJob> resumeJob(RequestScheduleModel scheduleJob) {
        scheduleJobService.resumeJob(scheduleJob.getJobId());
        return BaseResultResponse.ok();
    }

    /**
     * 删除定时器
     */
    @PostMapping("/deleteJob")
    public BaseResultResponse deleteJob(RequestScheduleModel scheduleJob) {
        scheduleJobService.delete(scheduleJob.getJobId());
        return BaseResultResponse.ok();
    }
}
